package logic

import (
	"context"
	"teatch/model/mysql"
	"teatch/teacherrpc/teacherrpcclient"

	"teatch/teacherrpc/internal/svc"
	"teatch/teacherrpc/teacherrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CourseListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCourseListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CourseListLogic {
	return &CourseListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CourseListLogic) CourseList(in *teacherrpc.CourseListRequest) (*teacherrpc.CourseListResponse, error) {
	// todo: add your logic here and delete this line
	offset := (in.Page - 1) * in.Size
	var Course []mysql.Course
	tx := mysql.DB.Offset(int(offset)).Limit(int(in.Size)).Order("created_at desc")
	if in.Title != "" {
		tx.Where("title like ?", "%"+in.Title+"%")
	}
	if in.Types != "" {
		tx.Where("types like ?", "%"+in.Types+"%")
	}
	tx.Find(&Course)

	var CourseList []*teacherrpc.CourseList
	for _, cour := range Course {
		list := teacherrpcclient.CourseList{
			TeachersId: int64(cour.TeachersId),
			Title:      cour.Title,
			Types:      cour.Types,
			Content:    cour.Content,
		}
		CourseList = append(CourseList, &list)
	}

	return &teacherrpc.CourseListResponse{
		List: CourseList,
	}, nil
}
